#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QString>
#include <QDir>
#include <QTextDocumentWriter>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    model=new QFileSystemModel();
    ui->treeView->setModel(model);
    model->setRootPath("c:/");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
   name=model->filePath(index);

            QDir n(name);
            QStringList filtr;
            filtr<<"*.txt";
    QStringList name1=n.entryList(filtr);
     modelstr=new QStringListModel(name1);
    ui->listView->setModel(modelstr);
    qDebug()<<name;
}

void MainWindow::on_listView_doubleClicked(const QModelIndex &index)
{
   QFile qwerty(name);
   if (!qwerty.open(QFile::ReadWrite|QFile::Text))
   {
       return;
   }
   QTextStream in(&qwerty);
   QString ytrewq=in.readAll();

   ui->textEdit->setText(ytrewq);

   qwerty.close();
   }







void MainWindow::on_listView_clicked(const QModelIndex &index)
{
       QString txtPath = QDir(name).absoluteFilePath(index.data().toString());

    QFile qwerty(txtPath);
    if (!qwerty.open(QFile::ReadWrite|QFile::Text))
    {
        return;
    }
    QTextStream in(&qwerty);
    QString ytrewq=in.readAll();

    ui->textEdit->setText(ytrewq);

    qwerty.close();
}

void MainWindow::on_pbSave_clicked(const QModelIndex &index)
{
    QString txtPath = QDir(name).absoluteFilePath(index.data().toString());
    QTextDocumentWriter writeTXT(txtPath,"plaintext");
    writeTXT.write(ui->textEdit->document());
}
